package com.example.administrator.x2picturetransliteration.Dao.Rx;


import com.example.administrator.x2picturetransliteration.Dao.GreenDaoManager;
import com.example.administrator.x2picturetransliteration.Dao.Table.GuidePage;

import org.greenrobot.greendao.rx.RxQuery;

import java.util.List;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2019/8/16.
 */

public class GuidePageRx {


    private CompositeSubscription mCompositeSubscriptions;

    public GuidePageRx(){

        mCompositeSubscriptions = new CompositeSubscription();
    }

    //添加数据库
    public Observable<GuidePage> insertData(GuidePage mGuidePage) {
        return GreenDaoManager.getInstance().getSession().getGuidePageDao().rx().insert(mGuidePage);
    }

    //添加数据至数据库，如果有则覆盖
    public void saveData(final GuidePage mGuidePage, final CallbackGuidePageRx mCallbackGuidePageRx) {

        mCompositeSubscriptions.add(
                queryAll()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<List<GuidePage>>() {

                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                                mCompositeSubscriptions.unsubscribe();
                            }


                            @Override
                            public void onNext(List<GuidePage> mGuidePages) {
                                if(mGuidePages.size()==0){

                                    mCompositeSubscriptions.add(
                                            insertData(mGuidePage)
                                                    .subscribeOn(Schedulers.io())
                                                    .observeOn(AndroidSchedulers.mainThread())
                                                    .subscribe(new Observer<GuidePage>() {


                                                        @Override
                                                        public void onCompleted() {

                                                        }

                                                        @Override
                                                        public void onError(Throwable e) {
                                                            mCompositeSubscriptions.unsubscribe();
                                                        }



                                                        @Override
                                                        public void onNext(GuidePage mGuidePages) {

                                                            mCompositeSubscriptions.unsubscribe();
                                                            mCallbackGuidePageRx.Method();
                                                        }
                                                    })
                                    );

                                }
                                else{

                                    mCompositeSubscriptions.add(
                                            updateData(mGuidePage)
                                                    .subscribeOn(Schedulers.io())
                                                    .observeOn(AndroidSchedulers.mainThread())
                                                    .subscribe(new Observer<GuidePage>() {


                                                        @Override
                                                        public void onCompleted() {

                                                        }

                                                        @Override
                                                        public void onError(Throwable e) {
                                                            mCompositeSubscriptions.unsubscribe();
                                                        }


                                                        @Override
                                                        public void onNext(GuidePage mGuidePage) {

                                                            mCompositeSubscriptions.unsubscribe();
                                                            mCallbackGuidePageRx.Method();
                                                        }
                                                    })
                                    );

                                }


                            }
                        })
        );
    }

    //通过事物添加数据库
    public Observable<Iterable<GuidePage>> insertData(List<GuidePage> list) {
        if (null == list || list.size() <= 0) {
            return Observable.error(new Throwable("null"));
        }
        return GreenDaoManager.getInstance().getSession().getGuidePageDao().rx().insertInTx(list);
    }


    //删除数据
    public Observable<Void> deleteData(GuidePage mGuidePage) {
        return GreenDaoManager.getInstance().getSession().getGuidePageDao().rx().delete(mGuidePage);
    }

    //删除所有数据
    public Observable<Void> deleteAll() {
        return GreenDaoManager.getInstance().getSession().getGuidePageDao().rx().deleteAll();
    }

    //更新数据
    public Observable<GuidePage> updateData(GuidePage mGuidePage) {
        return GreenDaoManager.getInstance().getSession().getGuidePageDao().rx().update(mGuidePage);
    }


    //查询所有数据
    public Observable<List<GuidePage>> queryAll() {
        RxQuery<GuidePage> rxQuery = GreenDaoManager.getInstance().getSession().getGuidePageDao().queryBuilder().rx();
        return rxQuery.list();
    }

    public interface CallbackGuidePageRx{

        public void Method();

    }


}
